
package com.sausageking.client.consumer.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.sausageking.client.consumer.gin.DealViewFactory;
import com.sausageking.shared.Merchant;
import com.sausageking.shared.Offer;

public class MerchantViewImpl extends Composite implements MerchantView {

  interface MerchantViewUiBinder extends UiBinder<Widget, MerchantViewImpl> {
  }

  private static MerchantViewUiBinder uiBinder = GWT
      .create(MerchantViewUiBinder.class);

  @UiField
  Label businessLabel;

  @UiField
  FlexTable dealTable;

  private Presenter presenter;

  private DealViewFactory dealViewFactory;

  @Inject
  public MerchantViewImpl(Presenter presenter, DealViewFactory dealViewFactory) {
    this.presenter = presenter;
    this.presenter.setView(this);
    this.dealViewFactory = dealViewFactory;
    initWidget(uiBinder.createAndBindUi(this));
  }

  @Override
  public void setMerchant(final Merchant merchant) {
    businessLabel.setText(merchant.getName());
  }

  @Override
  public void clearDeals() {
    dealTable.removeAllRows();
  }

  @Override
  public Presenter getPresenter() {
    return presenter;
  }

  @Override
  public DealView addDeal(Offer offer) {
    DealView view = dealViewFactory.createDealView(presenter).setOffer(offer);
    dealTable.setWidget(dealTable.getRowCount(), 0, view);
    return view;
  }
}
